package com.caac.core.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright Intolong 2004</p>
 * <p>Company: Intolong</p>
 * @author <a href="mailto:intolong@163.com">Intolong</a>
 * @version 1.0
 */

public abstract class AbstractTreeBean implements TreeBean {
    private Map flatMap;
    private Map nodeMap;
    private TreeNode root;

    public AbstractTreeBean(List list) {
        flatMap = new HashMap();
        nodeMap = new HashMap();
        root = new TreeNode(KEY_ROOT, null);
        for (int i = 0; i < list.size(); i++) {
            addBean(flatMap, list.get(i));
        }
        for (int i = 0; i < list.size(); i++) {
            addNode(list.get(i));
        }
    }

    protected abstract void addBean(Map flatMap, Object bean);

    protected abstract void addNode(Object bean);

    public List getRootChildren() {
        return root.getChildren();
    }

    public List getChildren(Object parentKey) {
        return getNode(parentKey).getChildren();
    }

    public Object getBean(Object key) {
        return flatMap.get(key);
    }

    public TreeNode getRootNode() {
        return root;
    }

    public TreeNode getNode(Object key) {
        return (TreeNode) nodeMap.get(key);
    }

    protected boolean containsKey(Object key) {
        return nodeMap.containsKey(key);
    }

    protected TreeNode createNode(Object key, Object bean) {
        TreeNode result = new TreeNode(key, bean);
        nodeMap.put(key, result);
        return result;
    }
}
